import {createStore} from "vuex";

const store = createStore({
    state(){
        return{
            shopCart:[],
            shopCartCheck:true,
            total:0,
            block:true,
            // payForList:[]
        }
    },
    getters:{

    },
    mutations:{
        shopCart(state,shopCart){
            const index = state.shopCart.findIndex(item=>{
                return item.name === shopCart.name
            })
            if(index === -1){
                state.shopCart.unshift(shopCart)
            }else{
                state.shopCart[index].count++
            }
        },
        increament(state,i){
            state.shopCart.findIndex((item)=>{
                if(i.name === item.name) {
                    item.count++
                }
            })
        },
        decreament(state,i){
            state.shopCart.findIndex((item)=>{
                if(i.name === item.name) {
                    if(item.count>1){
                        item.count--
                    }
                }
            })
        },
        heJi(state,item){

        },
        blockNone(state,block){
            state.block = block
            console.log(state.block)
        },
        deleteShopCart(state){
            state.shopCart
        },
        // payForList(state,item){
        //     state.payForList.push(item)
        // }
    },
    actions:{
        shopCart(context,shopCart){
            context.commit('shopCart',shopCart)
        },
        increament(context,item){
            context.commit('increament',item)
        },
        decreament(context,item){
            context.commit('decreament',item)
        },
        heJi(context,item){
            context.commit('heJi',item)
            console.log('action')
        },
        blockNone(context,block){
            context.commit('blockNone',block)
            // console.log(block)
        },
        deleteShopCart(context){
            context.commit('deleteShopCart')
        },
        // payForList(context,item){
        //     context.commit('payForList',item)
        //     console.log(item)
        // }
    }
})

export default store